package com.suyuan.controller.shop.admin;


import com.suyuan.entity.ProducetypeInfo;
import com.suyuan.service.ProductType2Service;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/shop/admin/Type")
public class ProductType2Controller {


    @Resource
    private ProductType2Service productType2Service;


    /**
     * 查找所有的的分类
     * @param modelMap
     * @return
     */
    @RequestMapping("list")
    public String listType(ModelMap modelMap){
        List<ProducetypeInfo> producetypeInfoList = productType2Service.findAllType();
        modelMap.addAttribute(producetypeInfoList);
        return "type_list";
    }


    /**
     * 删除
     * @param id
     * @return
     */
    @RequestMapping("delete")
    public String deleteType(Integer id){
        productType2Service.deleteType(id);
        return "redirect:/shop/admin/Type/list";
    }


    /**
     * 跳转编辑页面
     * @param id
     * @param modelMap
     * @return
     */
    @RequestMapping("toedit")
    public String toEditType(Integer id,ModelMap modelMap){
        ProducetypeInfo producetypeInfo = productType2Service.selectTypeByGid(id);
        modelMap.addAttribute(producetypeInfo);
        return "type_edit";
    }


    /**
     * 编辑商品信息类
     * @param producetypeInfo
     * @return
     */
    @RequestMapping("edit")
    public String editType(ProducetypeInfo producetypeInfo) throws Exception {
        try{
            productType2Service.updateTypeInfo(producetypeInfo);
        }catch (Exception e){
            throw new Exception("更新失败");
        }
        return "redirect:/shop/admin/Type/list";
    }

    /**
     * 跳转添加页面
     * @return
     */
    @RequestMapping("toAdd")
    public String toAddPage(){
        return "type_add";
    }


    /**
     * 添加商品信息
     * @param producetypeInfo
     * @return
     * @throws Exception
     */
    @RequestMapping("add")
    public String addType(ProducetypeInfo producetypeInfo) throws Exception {
        try{
            productType2Service.addProduceTypeInfo(producetypeInfo);
        }catch (Exception e ){
            throw new Exception("添加失败，请正确填写信息");
        }
        return "redirect:/shop/admin/Type/list";
    }



}
